package cn.com.cennavi.test;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import com.sun.org.apache.xml.internal.utils.URI;

import sun.misc.BASE64Encoder;

import javax.ws.rs.core.MediaType;

import java.io.*;
import java.net.URISyntaxException;
import java.util.HashSet;

import static java.lang.System.in;

/**
 * Created by fenggeng on 2017/3/7.
 */
public class test {
	static String inFile = "d://data/testdata";
	static String outFile = "d://data/outtestdata4";

	public static void testHelloService() throws Exception {
		Client client = Client.create();
		URI u = new URI("http://123.207.124.207:8083/mapspotter/odupload/oddata/upload");
		System.out.println(u);
		MultivaluedMapImpl params = new MultivaluedMapImpl();
		WebResource resource = client.resource(String.valueOf(u));
		String buf = "inputstream content.";
		params.add("userid", "666");
		params.add("modelid", "666");
		params.add("areaBound", "115.375,39.58333,117.5,40.5");

		String filestring = encodeBase64File(inFile);
		params.add("ins", filestring);
		String result = resource.type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, params);
		System.out.println(result);
	}

	public static byte[] File2byte(String filePath) {
		byte[] buffer = null;
		try {
			File file = new File(filePath);
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer;
	}

	public static void main(String args[]) throws IOException, URISyntaxException {
		datafilter(inFile, outFile);
		try {
			testHelloService();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void datafilter(String inFile, String outFile) throws IOException {
		// TODO Auto-generated method stub
		File file = new File(inFile);
		BufferedReader bf = new BufferedReader(new FileReader(new File(inFile)));
		BufferedWriter out = new BufferedWriter(new FileWriter(new File(outFile)));
		String line = "";
		bf.readLine();
		HashSet<String> odSet = new HashSet<String>();
		while ((line = bf.readLine()) != null) {
			String[] arr = line.split("\t");
			String olonlat = arr[1];
			String dlonlat = arr[2];
			String key = olonlat + dlonlat;
			if (odSet.contains(key)) {
				System.out.println(key);
			} else {
				odSet.add(key);
			}
//			if (olonlat.equals(dlonlat)) {
//				out.write(line);
//				System.out.println(line);
//				out.newLine();
//				out.flush();
//			}
		}
		bf.close();
		out.close();
	}

	public static String encodeBase64File(String path) throws Exception {
		File file = new File(path);
		FileInputStream inputFile = new FileInputStream(file);
		byte[] buffer = new byte[(int) file.length()];
		inputFile.read(buffer);
		inputFile.close();
		return new BASE64Encoder().encode(buffer);

	}
}
